
package org.upt.dcda.presentation;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.resteasy.util.HttpResponseCodes;
import org.upt.dcda.SearchManagerBeanRemote;
import org.upt.dcda.db.BookEntity;
import org.upt.dcda.exceptions.BookstoreException;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet
{

	/**
	 * 
	 */
	private static final long		serialVersionUID	= 5951524790502300958L;

	@EJB
	private SearchManagerBeanRemote	searchManagerBean;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TestServlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		try
		{
			List<BookEntity> ret = searchManagerBean.browse("tag1", "tag2");

			response.getOutputStream().println("START");

			for (BookEntity be : ret)
			{
				response.getOutputStream().println(be.getBookTitle());
			}

			response.getOutputStream().println("END");
		}
		catch (BookstoreException e)
		{
			getServletContext().log(e.toString(), e);
			response.sendError(HttpResponseCodes.SC_BAD_REQUEST, e.toString());
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException
	{
		// TODO Auto-generated method stub
	}

}
